package com.argrace.smart.callback;

import android.content.Context;

//import com.asun.jiawo.customer.dialog.ConnectionToast;
//import com.asun.jiawo.utils.ToolsKit;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okgo.request.BaseRequest;

import org.json.JSONObject;

//import cn.rongcloud.im.server.widget.LoadDialog;
import okhttp3.Call;
import okhttp3.Response;

/**
 * ================================================
 * 作    者：廖子尧
 * 版    本：1.0
 * 创建日期：2016/9/11
 * 描    述：返回字符串类型的数据
 * 修订历史：
 * ================================================
 */
public abstract class StringFrameCallback extends AbsCallback<String> {

    //    private LoadDialog mDialog;
    private Context context;
    private boolean isToastShow;
    private boolean isDialogShow;

    @Override
    public void onBefore(BaseRequest request) {

        context = (Context) request.getTag();

//        if (ToolsKit.isNetworkAvailable(context)) {
//
//            if (!request.getIsShowDialog()) {
//                return;
//            }
//            if (request.getIsShowToast()) {
//                isToastShow = true;
//            } else {
//                isToastShow = false;
//            }
//            if (request.getIsShowDialog()) {
//                isDialogShow = true;
//            } else {
//                isDialogShow = false;
//            }
//            if (isDialogShow) {
//                mDialog.show(context);
//            }
//
//
//        } else {
//
////            if(!App.getInstance().bluetoothisSearching){
//                mDialog.dismiss(context);
////            }

//            ConnectionToast.ToastShowMid(context, "请检查是否联网");
//        }

    }


    @Override
    public void onError(Call call, Response response, Exception e) {
        super.onError(call, response, e);

//        if(!App.getInstance().bluetoothisSearching){
//            mDialog.dismiss(context);
//        }

//        if (isToastShow) {
//            ConnectionToast.ToastShowMid(context, e.getMessage());
//        }
    }


    @Override
    public String convertSuccess(Response response) throws Exception {
        String s = StringConvert.create().convertSuccess(response);
        response.close();
        JSONObject jsonObject = new JSONObject(s);
        String retCode = jsonObject.getString("retCode");
        String msg = jsonObject.getString("msg");
        if ("0".equals(retCode)) {
            String body = jsonObject.getString("body");
            return body;
        } else {
            throw new IllegalStateException(msg);
        }
    }

    @Override
    public void onAfter(String s, Exception e) {
        super.onAfter(s, e);
//        if(!App.getInstance().bluetoothisSearching){
//            mDialog.dismiss(context);
//        }

    }
}